// Rewrite Url
function get_alias(){
	//var theForm = document.appForm;
	var str_rewrite = document.getElementById("name").value;
	str_rewrite= str_rewrite.toLowerCase();
	
	str_rewrite= str_rewrite.replace(/à|ả|ã|á|ạ|ă|ằ|ẳ|ẵ|ắ|ặ|â|ầ|ẩ|ẫ|ấ|ậ/g,"a");
	str_rewrite= str_rewrite.replace(/è|ẻ|ẽ|é|ẹ|ê|ề|ể|ễ|ế|ệ/g,"e");
	str_rewrite= str_rewrite.replace(/ì|ỉ|ĩ|í|ị/g,"i");
	str_rewrite= str_rewrite.replace(/ò|ỏ|õ|ó|ọ|ô|ồ|ổ|ỗ|ố|ộ|ơ|ờ|ở|ỡ|ớ|ợ/g,"o");
	str_rewrite= str_rewrite.replace(/ù|ủ|ũ|ú|ụ|ư|ừ|ử|ữ|ứ|ự/g,"u");
	str_rewrite= str_rewrite.replace(/ỳ|ỷ|ỹ|ý/g,"y");
	str_rewrite= str_rewrite.replace(/đ/g,"d");
	str_rewrite= str_rewrite.replace(/!|@|\$|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\'| |\"|\&|\#|\[|\]|~/g,"-");
	str_rewrite= str_rewrite.replace(/-+-/g,"-");
	str_rewrite= str_rewrite.replace(/^\-+|\-+$/g,"");

	document.getElementById("alias").value = str_rewrite;
	
	return str;
}

/*
 * Submit form
 */
function OnSubmitForm(url)
{ 
	document.appForm.action = url;
	document.appForm.submit();
	return true;
}

function OnSubmitForm2(url)
{ 
	var theForm = document.appForm;
	var number = 0;
	for(var i = 0; i < theForm.cid.length; i++){
		if(theForm.cid[i].checked == true){
			number++;
		}
	}
	if(number == 0){
		alert('Bạn chưa chọn dữ liệu nào');
		return false;
	}
	theForm.action = url;
	theForm.submit();
	return true;
}

/*
 * Check all
 */
function checkCheckBox(){
	var theForm = document.appForm;
	if (theForm.elements[i].name=='cid[]')
	{
        theForm.elements[i].checked = checked;
        if(theForm.elements[i].checked = true){
        	window.alert(this.value);
        }
    }
}

var checked=false;
function checkedAll() {
    var theForm = document.appForm;
    if (checked == false)
    {
    	checked = true;
    }
    else
    {
    	checked = false;
    }
    
    var countCheckBox = 0;
    for (var i = 0; i<theForm.elements.length; i++) {
        if (theForm.elements[i].name=='cid[]'){
            theForm.elements[i].checked = checked;
            countCheckBox++;
        }
    }
    
    if (checked == true)
    {
    	theForm.checkValue.value = countCheckBox;
    }
    else
    {    	
    	theForm.checkValue.value = 0;
    }
}

//Loading page
function loadingPage(){
	$('html').append('<div class="loading_page" id="loading_page"></div>');
}
function unLoadingPage(){
	$('#loading_page').fadeOut('fast').remove();
}

//Change Status
function ajaxLoad(url, idLoad){
	$('#' + idLoad).html('');
	$('#' + idLoad).addClass('dangtai');
	return false;
}

// Xoa trang noi dung
function xoaTrangDiv(id){
	$('#' + id).text('');
}
function xoaTrangInput(id){
	$('#' + id).val('');
}


// Ajax load Marketing
function loadPageMarketing(idDiv){
	var linkAjax = base_url + '/marketing/ajax/list-marketing';
	$('#dialog-form').text('').load( linkAjax, function(){ 
		$(this).removeClass('dangtai2');
		if($('#' + idDiv + ' .item').length > 0){
			$('#tblDaChon').append('<tr><th>Tên chiến dịch</th><th style="text-align: center;"><a href="javascript:void(0)" onclick="xoaChonMarketingAll()">Xóa tất cả</a></th></tr>');
			$('#' + idDiv + ' .item').each(function(index){
				$('#tblDaChon tr:last').after('<tr id="'+$(this).attr('id')+'"><td id="name"><a href="javascript:coid(0)" title="'+ $(this).attr('title') +'">' + $(this).text() + '</a></td><td align="center"><a href="javascript:void(0)" onclick="xoaChonMarketing('+$(this).attr('id')+')">Xóa</a></td></tr>');	
			});
		} 
	} );
}
function showListMarketing(idDiv, idValue){
	$('body').append('<div id="dialog-form" class="dangtai2" style="display: none;"></div>');
	loadPageMarketing(idDiv);
	$( "#dialog-form" ).dialog({
		autoOpen: true,
		width: 950,
		height: 580,
		modal: true,
		title: 'Danh sách chiến dịch marketing',
		open: function(){},
		buttons: {
			"Lưu": function() {
				$('#' + idDiv).html('');
				$('#tblDaChon tr').each(function(index){
					if(index > 0){
						$('#' + idDiv).append('<span class="item goc3" id="'+$(this).attr('id')+'" title="'+$('#name a', this).attr('title')+'">'+$('#name', this).text()+'<a href="javascript:void(0)" class="delete" title="Xóa" onclick="xoaMotMarketing('+$(this).attr('id')+', \''+idDiv+'\')"></a></span>');
						$('#' + idDiv).append('<input id="'+$(this).attr('id')+'" type="hidden" name="'+idValue+'[]" value="'+$(this).attr('id')+'">');
					}
				});
				$( this ).dialog( "close" ).remove();
			},
			"Đóng": function() {
				$( this ).dialog( "close" ).remove();
			},
			"Tải lại": function() {
				loadPageMarketing(idDiv);
			}
		},
		close: function() {
			$(this).remove();
		}
	});
}

function xoaMotMarketing(idMarketing, idDiv){
	$('#' + idDiv + ' #' + idMarketing).remove();
}
// End Marketing


// Ajax load user
function loadPageUser(idDiv){
	var linkAjax = base_url + '/default/ajax/list-user';
	$('#dialog-form').text('').load( linkAjax, function(){ 
		$(this).removeClass('dangtai2');
		if($('#' + idDiv + ' .item').length > 0){
			$('#tblDaChon').append('<tr><th>Họ tên</th><th style="text-align: center;"><a href="javascript:void(0)" onclick="xoaChonUserAll()">Xóa tất cả</a></th></tr>');
			$('#' + idDiv + ' .item').each(function(index){
				$('#tblDaChon tr:last').after('<tr id="'+$(this).attr('id')+'"><td id="name"><a href="javascript:coid(0)" title="'+ $(this).attr('title') +'">' + $(this).text() + '</a></td><td align="center"><a href="javascript:void(0)" onclick="xoaChonUser('+$(this).attr('id')+')">Xóa</a></td></tr>');	
			});
		} 
	} );
}
function showListUser(idDiv, idValue){
	$('body').append('<div id="dialog-form" class="dangtai2" style="display: none;"></div>');
	loadPageUser(idDiv);
	$( "#dialog-form" ).dialog({
		autoOpen: true,
		width: 950,
		height: 580,
		modal: true,
		title: 'Danh sách nhân viên',
		open: function(){},
		buttons: {
			"Lưu": function() {
				$('#' + idDiv).html('');
				$('#tblDaChon tr').each(function(index){
					if(index > 0){
						$('#' + idDiv).append('<span class="item goc3" id="'+$(this).attr('id')+'" title="'+$('#name a', this).attr('title')+'">'+$('#name', this).text()+'<a href="javascript:void(0)" class="delete" title="Xóa" onclick="xoaMotUser('+$(this).attr('id')+', \''+idDiv+'\')"></a></span>');
						$('#' + idDiv).append('<input id="'+$(this).attr('id')+'" type="hidden" name="'+idValue+'[]" value="'+$(this).attr('id')+'">');
					}
				});
				$( this ).dialog( "close" ).remove();
			},
			"Đóng": function() {
				$( this ).dialog( "close" ).remove();
			},
			"Tải lại": function() {
				loadPageUser(idDiv);
			}
		},
		close: function() {
			$(this).remove();
		}
	});
}

function xoaMotUser(idUser, idDiv){
	$('#' + idDiv + ' #' + idUser).remove();
}
// End load user

// Ajax load Khoa hoc
function loadPageKhoahoc(idDiv){
	var linkAjax = base_url + '/daotao/ajax/ajax-list-khoahoc';
	$('#dialog-form').text('').load( linkAjax, function(){ 
		$(this).removeClass('dangtai2');
		if($('#' + idDiv + ' .item').length > 0){
			$('#tblDaChon').append('<tr><th>Tên chiến dịch</th><th style="text-align: center;"><a href="javascript:void(0)" onclick="xoaChonMarketingAll()">Xóa tất cả</a></th></tr>');
			$('#' + idDiv + ' .item').each(function(index){
				$('#tblDaChon tr:last').after('<tr id="'+$(this).attr('id')+'"><td id="name"><a href="javascript:coid(0)" title="'+ $(this).attr('title') +'">' + $(this).text() + '</a></td><td align="center"><a href="javascript:void(0)" onclick="xoaChonMarketing('+$(this).attr('id')+')">Xóa</a></td></tr>');	
			});
		} 
	} );
}
function showListKhoahoc(idDiv, idValue){
	$('body').append('<div id="dialog-form" class="dangtai2" style="display: none;"></div>');
	loadPageKhoahoc(idDiv);
	$( "#dialog-form" ).dialog({
		autoOpen: true,
		width: 950,
		height: 580,
		modal: true,
		title: 'Danh sách Khóa học',
		open: function(){},
		buttons: {
			"Lưu": function() {
				$('#' + idDiv).html('');
				$('#tblDaChon tr').each(function(index){
					if(index > 0){
						$('#' + idDiv).append('<span class="item goc3" id="'+$(this).attr('id')+'" title="'+$('#name a', this).attr('title')+'">'+$('#name', this).text()+'<a href="javascript:void(0)" class="delete" title="Xóa" onclick="xoaMotMarketing('+$(this).attr('id')+', \''+idDiv+'\')"></a></span>');
						$('#' + idDiv).append('<input id="'+$(this).attr('id')+'" type="hidden" name="'+idValue+'[]" value="'+$(this).attr('id')+'">');
					}
				});
				$( this ).dialog( "close" ).remove();
			},
			"Đóng": function() {
				$( this ).dialog( "close" ).remove();
			},
			"Tải lại": function() {
				loadPageKhoahoc(idDiv);
			}
		},
		close: function() {
			$(this).remove();
		}
	});
}

function xoaMotKhoahoc(idMarketing, idDiv){
	$('#' + idDiv + ' #' + idMarketing).remove();
}
// End load Khoa hoc



jQuery(function($) {
	$('.auto_numberic').autoNumeric('init', { mDec: 0, aDec: ',', aSep: '.', vMax: '10000000000'});
	$('.auto_price').autoNumeric('init', {vMin:'-9999999999', vMax:'9999999999', aSign :' VNĐ', pSign : 's', aSep: '.', aDec: ','});
	$(".auto_init").keypress(function (e){
		var charCode = (e.which) ? e.which : e.keyCode;
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			return false;
	  	}
	});
});


$(document).ready(function(){
	//Loading page
	/*$('a').click(function(){
		loadingPage();
	});*/
    
	// Can bang chieu cao cot trai va phai
	/*var height_left = $('.left_content').outerHeight();
	var height_right = $('.right_content').outerHeight();
	if(height_right > height_left){
		$('.left_content').css({'height':height_right + 'px'});
	}*/
	
	// Thu nho box
	$('.toggleBlock').click(function () {
		var id = $(this).attr('id');
		if($("div#" + id).is(':hidden')){
			$(this).removeClass('db-down').addClass('db-up');
		}else{
			$(this).removeClass('db-up').addClass('db-down');
		}
		$("div#" + id).slideToggle();
	});
	
	// Tab Ui
	$(function() {
		$( "#tabs" ).tabs();
	});
	
	// Hien thong bao loi
	$("div.listError").each(function (i) {
        $('#view_' + $(this).attr('data')).html($(this).attr('rel'));
    });
	
	// Hien thi ghi chu
	$( 'input,a,textarea,select' ).tooltip({
		 position: {
			 my: "center bottom-15",
			 at: "center top",
			 using: function( position, feedback ) {
				 $( this ).css( position );
				 $( "<div>" )
				 .addClass( "ui-arrow" )
				 .addClass( feedback.vertical )
				 .addClass( feedback.horizontal )
				 .appendTo( this );
			 }
		 },
		 show: {
			 duration: "slow"
		 },
		 hide: {
			 effect: "hide"
		 }
	});
	
	//date
	$(".hasDatePicker").datepicker({ 
		dateFormat: 'dd/mm/yy',
		changeMonth: true,
		changeYear: true,
		yearRange: "-100:+2"
	});
	
	// Chan keydown enter search
	$('#keywords').keydown(function(event) {
		if (event.keyCode == 13) {
			return false;
		}
	});
});


